package com.bootdo.common.task;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaSubscribeMessage;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.bootdo.common.config.WxMaConfiguration;
import com.bootdo.common.config.WxMaProperties;
import com.bootdo.common.dao.TaskDao;
import com.bootdo.common.utils.DateUtils;
import com.bootdo.wx.constant.Constants;
import com.bootdo.wx.dao.*;
import com.bootdo.wx.domain.CmsNoticeDO;
import com.bootdo.wx.domain.GoodsLiveAppointmentDO;
import com.bootdo.wx.domain.GoodsLiveDO;
import com.bootdo.wx.domain.MemberUserDO;
import me.chanjar.weixin.common.error.WxErrorException;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
public class LiveKeepJop implements Job{

    @Autowired
    private GoodsLiveDao goodsLiveDao;
    @Autowired
    private GoodsLiveAppointmentDao goodsLiveAppointmentDao;
    @Autowired
    private WxMaProperties properties;
    @Autowired
    private MemberUserDao memberUserDao;
    @Autowired
    private TaskDao taskDao;
    @Autowired
    private CmsNoticeDao cmsNoticeDao;

    @Override
    public void execute(JobExecutionContext arg0) throws JobExecutionException {


        String group = arg0.getTrigger().getJobKey().getGroup();
        System.out.println("group:"+group);
        String name = arg0.getTrigger().getJobKey().getName();
        System.out.println("name:"+name);


        System.out.println("开始预约通知定时任务");
        //获取所有10分钟之内的直播商品,也就是当前时间
        GoodsLiveDO goodsLiveDO = goodsLiveDao.get(group);
        if(null == goodsLiveDO){
            return;
        }

        final WxMaService wxService = WxMaConfiguration.getMaService(properties.getConfigs().get(0).getAppid());
        WxMaSubscribeMessage subscribeMessage = new WxMaSubscribeMessage();

            //给用户发订阅消息

            //获取预约用户
           List<GoodsLiveAppointmentDO> goodsLiveAppointmentList = goodsLiveAppointmentDao.getAppointmentUserListByLiveId(goodsLiveDO.getId());

           if(goodsLiveAppointmentList.size()>0){
               for (GoodsLiveAppointmentDO goodsLiveAppointmentDO : goodsLiveAppointmentList) {


               //订单取消通知

                   //支付成功提醒

                   //获取用户信息
                   MemberUserDO memberUserDO = memberUserDao.get(goodsLiveAppointmentDO.getUserId());


                   //发送订阅消息
                   subscribeMessage.setToUser(memberUserDO.getWechatOpenId());
                   subscribeMessage.setTemplateId(Constants.mesage.MODE_LIVE_STRAT);
                   subscribeMessage.setPage(Constants.mesage.address.ADDRESS_LIVE_APPOINTMENT);

                   List<WxMaSubscribeMessage.Data> list = new ArrayList<>();
                   for (int i = 0; i < 4; i++) {
                       WxMaSubscribeMessage.Data data = new WxMaSubscribeMessage.Data();
                       switch (i) {
                           case 0:
                               data.setName("thing1");
                               data.setValue("视频号直播");
                               break;
                           case 1:
                               data.setName("time2");
                               data.setValue(DateUtil.format(goodsLiveDO.getStarAt(),DateUtils.DATE_TIME_PATTERN));
                               break;
                           case 2:
                               data.setName("thing3");
                               data.setValue("您预约的直播活动即将开始,请及时参加");
                               break;
                           case 3:
                               data.setName("date2");
                               data.setValue(DateUtil.format(new Date(), DateUtils.DATE_TIME_PATTERN));
                               break;

                           default:
                               break;
                       }
                       list.add(data);


                   }
                   try {
                       subscribeMessage.setData(list);
                       wxService.getMsgService().sendSubscribeMsg(subscribeMessage); //发送订阅消息
                   } catch (WxErrorException e) {
                         e.printStackTrace();

                       //往平台公告表加入数据
                       CmsNoticeDO cmsNoticeDO = new CmsNoticeDO();
                       cmsNoticeDO.setId(IdUtil.simpleUUID());

                       cmsNoticeDO.setContent("您预约得直播,还有10分钟就要开始了,记得不要错过");
                       cmsNoticeDO.setDelFlag(0);
                       cmsNoticeDO.setCaAt(new Date());
                       cmsNoticeDO.setTitle("退货通知");
                       cmsNoticeDO.setUserId(memberUserDO.getId());
                       cmsNoticeDao.save(cmsNoticeDO);

               }



               }
           }


        taskDao.deleteByGroup(group);

        }



        //orderOrderService.receivingOrderJob();


}